package com.zdw.template.config.international;

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

@Component
public class InternationalUtils {

    private static MessageSource messageSource;

    public InternationalUtils(MessageSource messageSource) {
        InternationalUtils.messageSource = messageSource;
    }

    /**
     * 获取国际化/多语言的实际返回值
     *
     * @param key
     * @return
     */
    public static String getInternational(String key) {
        try {
            System.out.println("国际化/多语言messageSource:" + messageSource.toString());
            return messageSource.getMessage(
                    key,
                    null,
                    LocaleContextHolder.getLocale());
        } catch (Exception e) {
            return key;
        }
    }

}
